package com.skaz.util;

import org.springframework.http.MediaType;

import java.nio.charset.Charset;

/**
 * SS4
 * 带UTF-8 charset 定义的MediaType.
 * Jax-RS和Spring的MediaType没有UTF-8的版本，
 * Google的MediaType必须再调用toString()函数而不是常量，不能用于Restful方法的annotation。
 */
public class MediaTypes {

    public static final String TEXT_PLAIN       = "text/plain";
    public static final String TEXT_PLAIN_UTF_8 = "text/plain;charset=UTF-8";

    public static final String TEXT_HTML       = "text/html";
    public static final String TEXT_HTML_UTF_8 = "text/html;charset=UTF-8";

    public static final String APPLICATION_JSON       = "application/json";
    public static final String APPLICATION_JSON_UTF_8 = "application/json;charset=UTF-8";

    public static final String TEXT_XML       = "text/xml";
    public static final String TEXT_XML_UTF_8 = "text/xml;charset=UTF-8";

    public static final String APPLICATION_XML       = "application/xml";
    public static final String APPLICATION_XML_UTF_8 = "application/xml; charset=UTF-8";

    public static final String APPLICATION_JAVASCRIPT       = "application/javascript";
    public static final String APPLICATION_JAVASCRIPT_UTF_8 = "application/javascript; charset=UTF-8";

    public static final String APPLICATION_XHTML_XML       = "application/xhtml+xml";
    public static final String APPLICATION_XHTML_XML_UTF_8 = "application/xhtml+xml; charset=UTF-8";

    public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";


    public static final MediaType APPLICATION_JSON_UTF_8_MediaType = newMediaType(APPLICATION_JSON_UTF_8);

    /**
     * 创建MediaType
     * 用new MediaType(APPLICATION_JSON_UTF_8_VALUE);将报错
     */
    public static MediaType newMediaType(String type) {
        return MediaType.parseMediaType(type);
    }

    /**
     * new MediaType("application", "json", Charsets.UTF_8)
     */
    public static MediaType newMediaType(String type, String subType, Charset charset) {
        return new MediaType(type, subType, charset);
    }
}
